<?php

namespace App\Http\Controllers\manage;

use Validator;
use App\managerModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class loginController extends Controller
{
    public function login(Request $request)
    {
        $request->session()->forget("user");
        return view('manage/login');
    }

    public function doLogin(Request $request)
    {
        $messages = [
            'user.required' => '您没有填写 用户名',
            'pwd.required' => '您没有填写 密码'
        ];

        $rules = [
            'user' => 'required',
            'pwd' => 'required'
        ];

        $validator = Validator::make($_REQUEST, $rules, $messages);
        $validator->validate();

        $user = $_REQUEST['user'];
        $pwd = $_REQUEST['pwd'];

        $account = managerModel::where("user", $user)->first();
        if ($account == null) {
            $validator->errors()->add('user', '用户不存在！');
            return back()->withErrors($validator);
        }
        if ($account->pwd == $pwd) {
            //用户身份验证成功，设置session(用户名)
            $request->session()->put("user", $user);
            return redirect("manage/index");
        } else {
            $validator->errors()->add('pwd', '密码错误！');
            return back()->withErrors($validator);
        }
    }
}
